﻿
Imports std
Imports webctrl
Imports storoomc

Public Module homeserve

    Public Const defaultConfigfile As String = "homeserve.ini"
    Public Const homeserveCounterKey As String = "homeserve_counter_6e65e6b7-03d3-42e6-b1a1-f45b444f4213"
    Public Const defaultLinkImage As String = "static?file=defaultImage.ico"

    Public ReadOnly csslinkhref As String = staticrequest.generateStaticfileLinkUrl("homepage.css")

    Public WithEvents server As processorHttpServer = Nothing
    Public WithEvents config As configMonitor = Nothing

    Public Function getstoroom(ByRef host As String, ByRef port As Int32, ByRef retrytimes As Int64) As Boolean
        host = config.parameter(configNaming.homeserve.name _
                              , configNaming.homeserve.storoomhost _
                              , configNaming.defaultValue.homeserve.storoomhost)
        port = config.parameter(configNaming.homeserve.name _
                              , configNaming.homeserve.storoomport _
                              , configNaming.defaultValue.homeserve.storoomport)
        retrytimes = config.parameter(configNaming.homeserve.name _
                                    , configNaming.homeserve.storoomretrytimes _
                                    , configNaming.defaultValue.homeserve.storoomretrytimes)

        Return True
    End Function

    Public Sub shutdown()
        If Not server Is Nothing Then
            server.stop()
        End If
    End Sub

    Public Sub unhandledException(ByVal ex As Exception)
        shutdown()
        finishmain()
    End Sub

    Public Sub startservice()
        shutdown()
        If server Is Nothing Then
            server = New processorHttpServer()
        End If

        server.addRequestProcessor(New favicon())
        server.addRequestProcessor(New staticrequest())
        server.addRequestProcessor(New verpage())
        server.addRequestProcessor(New buildInfoPage())
        server.addRequestProcessor(New blog())
        server.addRequestProcessor(New comment())
        server.addRequestProcessor(New counter())
        server.addRequestProcessor(New image())
        server.addRequestProcessor(New links())
        server.addRequestProcessor(New music())
        server.addRequestProcessor(New viewcomment())
        server.addRequestProcessor(New aboutme())
        server.addRequestProcessor(New products())

        server.addRequestProcessor(New homepage())

        Dim ports As String = Nothing
        ports = config.parameter(configNaming.homeserve.name, configNaming.homeserve.ports _
                                 , configNaming.defaultValue.homeserve.ports)
        server.addports(ports)
        raiseError("server will start on ports " + ports)

        server.start()
    End Sub

    Public Sub main(ByVal args() As String)
        startmain(AddressOf unhandledException)
        Dim configfile As String = defaultConfigfile
        If args.Length() > 0 Then
            configfile = args(0)
        End If
        config = New configMonitor()
        assert(config.open(configfile), "cannot open configfile " + configfile)
        storoom.initial(AddressOf getstoroom)

        startservice()

        While Not consoleExtSignal() AndAlso (server.health() OrElse config.refreshing())
            sleep()
        End While

        If consoleExtSignal() Then
            raiseError("caught console cancel key event, shutdown...")
        ElseIf Not server.health() Then
            raiseError("caught httpServer error, shutdown...", errorHandle.errorType.critical)
        Else
            assert(False, "caught unhandled exception, shutdown...")
        End If

        shutdown()
        finishmain()
        forceQuit(0)
    End Sub

    Private Sub server_receive(ByVal context As httpContext) Handles server.receive
        assert(False, "processorHttpServer should not raise receive event.")
    End Sub

    Private Sub config_ConfigChanged() Handles config.ConfigChanged
        startservice()
    End Sub
End Module
